﻿using System;
using System.Collections.Generic;
using System.Linq;
using MartinRL.ProjectEuler.Sequences;

namespace MartinRL.ProjectEuler
{
    /// <summary>
    /// http://projecteuler.net/index.php?section=problems&id=4
    /// </summary>
    public static class Problem004
    {
        public static int FindTheLargestPalindromeMadeFromTheProductOfTwo3DigitNumbers()
        {
            const int term = 999;
            var zeroTo999Sequence = 0.To(term);
            var zeroTo999Sequence2 = 0.To(term);

            return zeroTo999Sequence
                .SelectMany(t => zeroTo999Sequence2.Select(t2 => t * t2))
                .Where(IsPalindrome)
                .Max();
        }

        private static bool IsPalindrome(int term)
        {
            return term.ToString().SequenceEqual(term.ToString().Reverse());
        }
    }
}